package com.g2.admin.controller;

import com.g2.admin.model.po.UserAccountPo;
import com.g2.admin.model.to.ResultTo;
import com.g2.admin.service.UserAccountService;
import com.g2.admin.utils.RedisUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

@RestController
@Validated
@Api(tags = "Account")
public class LoginController extends BaseController {
    @Autowired
    private CaptchaController captchaController;
    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private RedisUtil<UserAccountPo> redisUtil;

    @PostMapping("/login")
    public String login(@RequestBody @Validated UserAccountPo userAccount) {
        Assert.isTrue(captchaController.verifyCaptcha(userAccount.getCode()), ResultTo.getCodeAndMsg(ResultTo.ResultCode.CODE_ERR));
        return userAccountService.login(userAccount.getUsername(), userAccount.getPassword());
    }

    @PostMapping("/logout")
    public void logout() {
        // session失效
        invalidate();
        // TODO token删除
//        redisUtil.delete(RedisConstants.getKeyToken());
    }
}
